<template>
  <div>
    <!--查询-->
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="标题">
        <el-input v-model="formInline.title" placeholder="标题"></el-input>
      </el-form-item>
      <el-form-item  label="分类名称">
        <el-select v-model="formInline.region" placeholder="请选择要查询的分类名称">
          <el-option label="所有分类" value=""></el-option>
          <el-option
            v-for="item in formInline.typelist"
            :key="item.id"
            :label="item.typename"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      ref="multipleTable"
      :data="homeList"
      tooltip-effect="dark"
      @selection-change="handleSelectionChange">
      <el-table-column
        align="center"
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        align="center"
        prop="id"
        label="ID"
        width="200">
      </el-table-column>
      <el-table-column
        align="center"
        prop="sort"
        label="排序"
        width="80">
      </el-table-column>
      <el-table-column
        align="center"
        prop="title"
        label="标题"
        width="250">
      </el-table-column>
      <el-table-column
        align="center"
        prop="typename"
        label="分类名称"
        width="150">
      </el-table-column>
      <el-table-column
        align="center"
        prop="imageUrl"
        label="封面"
        >
        <template slot-scope="scope">
          <img  :src="baseUrl + scope.row.image_url" alt="" style="width: 50px;height: 50px">
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="newsTitle"
        label="文章标题"
      >
      </el-table-column>
      <el-table-column
        align="center"
        prop="desc"
        label="描述"
        >
      </el-table-column>
      <el-table-column label="操作" align="center" width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 20px">
      <el-button @click="batchDel()">批量删除</el-button>
      <el-button @click="toggleSelection()">取消选择</el-button>
    </div>

    <el-pagination
      v-show="isShowPagination"
      background
      layout="prev, pager, next"
      :current-page.sync="currentPage"
      @current-change="handleCurrentChange"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
export default {
  name: 'home-list',
  data () {
    return {
      currentPage: 1,
      loading: true,
      formInline: {
        title: '',
        typelist: [],
        region: ''
      },
      baseUrl: this.GLOBAL.BASE_URL + '/',
      homeList: [],
      multipleSelection: [],
      total: 0,
      isShowPagination: false
    }
  },
  methods: {
    toggleSelection (rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    },
    handleCurrentChange (val) {
      if (this.formInline.region !== '') {
        this.getHomeListByTypeId(val)
      } else {
        this.getHomeList(val)
      }
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    getHomeList (pageNumber) {
      this.$http.get('/home/getHomeList', {
        params: {
          pageNumber: pageNumber
        }
      }).then(res => {
        this.loading = false
        let data = res.data
        if (data.status === 200) {
          console.log(data)
          this.currentPage = data.data.pageNumber
          this.total = data.data.totalRow
          if (this.total > 0) {
            this.isShowPagination = true
          }
          this.homeList = data.data.list
        }
      })
    },
    handleDelete (index, row) {
      this.$http.get('/home/deleteHome', {
        params: {
          ids: row.id
        }
      }).then(res => {
        let data = res.data
        let status = data.status
        if (status === 200) {
          this.getHomeList(1)
        }
      })
    },
    handleEdit (index, row) {
      this.$router.push({
        name: 'EditHome',
        params: {
          id: row.id,
          title: row.title,
          desc: row.desc,
          newsTitle: row.newsTitle,
          newsId: row.news_id,
          imagePath: row.image_url,
          homeTypeId: row.home_type_id,
          homeTypeName: row.typename,
          sort: row.sort
        }
      })
    },
    // 批量删除
    batchDel () {
      if (this.multipleSelection) {
        let ids = this.multipleSelection.map(item => item.id).join()
        if (ids.length > 0) {
          this.$http.get('/home/deleteHome', {
            params: {
              ids: ids
            }
          }).then(res => {
            let data = res.data
            let status = data.status
            if (status === 200) {
              this.getHomeList(1)
            }
          })
        }
      }
    },
    onSubmit () {
      if (this.formInline.region !== '' && this.formInline.title !== '') {
        this.getHomeListByTypeTitleAndTypeId(1)
      } else if (this.formInline.region !== '') {
        this.getHomeListByTypeId(1)
      } else if (this.formInline.title !== '') {
        this.getHomeListByTypeTitle(1)
      }
    },
    getTypeList () {
      this.$http.get('/hometype/getHomeTypeList')
        .then(this.getTypeListInfoSucc)
    },
    getTypeListInfoSucc (res) {
      let data = res.data
      if (data.status === 200) {
        this.formInline.typelist = data.data
      }
    },
    getHomeListByTypeId (pageNumber) {
      this.currentPage = 1
      this.$http.get('/home/getHomeListByTypeId', {
        params: {
          typeId: this.formInline.region,
          pageNumber: pageNumber
        }
      }).then(res => {
        let data = res.data
        if (data.status === 200) {
          this.currentPage = data.data.pageNumber
          this.total = data.data.totalRow
          if (this.total > 0) {
            this.isShowPagination = true
          }
          this.homeList = data.data.list
        }
      })
    },
    getHomeListByTypeTitle (pageNumber) {
      this.currentPage = 1
      this.$http.get('/home/getHomeListByTitle', {
        params: {
          title: this.formInline.title,
          pageNumber: pageNumber
        }
      }).then(res => {
        let data = res.data
        if (data.status === 200) {
          this.currentPage = data.data.pageNumber
          this.total = data.data.totalRow
          if (this.total > 0) {
            this.isShowPagination = true
          }
          this.homeList = data.data.list
        }
      })
    },
    getHomeListByTypeTitleAndTypeId (pageNumber) {
      this.currentPage = 1
      this.$http.get('/home/getHomeListByTitleAndTypeId', {
        params: {
          title: this.formInline.title,
          typeId: this.formInline.region,
          pageNumber: pageNumber
        }
      }).then(res => {
        let data = res.data
        if (data.status === 200) {
          this.currentPage = data.data.pageNumber
          this.total = data.data.totalRow
          if (this.total > 0) {
            this.isShowPagination = true
          }
          this.homeList = data.data.list
        }
      })
    }
  },
  mounted () {
    this.getHomeList(1)
    this.getTypeList()
  }
}
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
.el-pagination{
  margin-top 50px
  text-align center
}
</style>
